/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.alphawavesgames.engine.rendering;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import javax.microedition.khronos.opengles.GL10;

/**
 *
 * @author duncan
 * Handles all textures.
 */
public abstract class TextureManager
{
    //texture array
    private List<Texture> m_textures;

    //a class to expose handles to the textures stored in the texture manager
    public class TextureHandle
    {
        private static final int INVALID_HANDLE = -1;
        //index into the texture array where the texture is stored
        private int m_textureIndex;
        
        public TextureHandle()
        {
            m_textureIndex = INVALID_HANDLE;
        }
    }

    public TextureManager()
    {
        m_textures = new ArrayList<Texture>();
    }

    public void initialise( RenderContainer renderContainer )
    {

    }

    public void finalise()
    {

    }

    public void update()
    {
       //go through all textures and give them a chance to update
        ListIterator<Texture> texIterator = m_textures.listIterator();

        while( texIterator.hasNext() )
        {
            Texture tex = texIterator.next();
            tex.update();
        }

    }

    public void recreateTextures()
    {
        //go through list and call recreate on each texture
    }

    public abstract TextureHandle createTexture( int assetID );

    //add a texture to the list
    public TextureHandle add( Texture tex )
    {
        //todo :  check if texture is already present before adding
        TextureHandle handle = new TextureHandle();
        handle.m_textureIndex = m_textures.size();

        assert( m_textures != null );

        boolean added = m_textures.add( tex );

        assert( added );

        return handle;
    }

    private boolean isValidHandle( TextureHandle handle )
    {
        if( handle.m_textureIndex >= 0 && handle.m_textureIndex < m_textures.size() )
        {
            return true;
        }

        return false;
    }

    public Texture getTexture( TextureHandle handle )
    {
        if( isValidHandle( handle ) )
        {
            return m_textures.get( handle.m_textureIndex );
        }
        return null;
    }



    public class Texture
    {
        //todo : add the internal image representation

        protected int m_width;
        protected int m_height;
        protected int m_bitsPerTexel;
        protected boolean m_hasAlpha;
        protected boolean m_pendingLoad;


        protected Texture()
        {
            m_width = m_height = 0;
            m_bitsPerTexel = 0;
            m_hasAlpha = false;
            m_pendingLoad = false;
        }

        //todo : fill in the methods according to the internal
        //image representation
        //load a texture given the filenam.
        //Return true if successful, false otherwise.
        public boolean load( String name )
        {
            return false;
        }

        public void recreate()
        {
            //
        }

        public void update()
        {

        }

    }

}
